<template>
	<view class="car-count">
		<view style="min-height: 100vh;">
			<view style="
				background-color: #fff;
				border-bottom-left-radius: 40rpx;
				border-bottom-right-radius: 40rpx;
				border-top: 2rpx solid #f5f5f5;
				height: 80rpx;display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 0 40rpx;
			">
				<view @click="openPage('pages/address/address', {source:1})" style="display: flex;flex-direction: row;align-items: center;">
					<u-icon name="map" size="28"></u-icon>
					<text style="margin-left: 10rpx;font-size: 24rpx;">{{addressName}}</text>
				</view>
				<view style="display: flex;flex-direction: row;align-items: center;">
					<u-line direction="col" color="#c5c5c5" length="40rpx"></u-line>
					<text style="margin-left: 50rpx;font-size: 24rpx;" @click="editGoods">{{delText}}</text>
				</view>
			</view>
			<view class="car-list">
				<view class="list" v-for="(itm,gIndex) in carList" :key="itm.id">
					<view style="display: flex;align-items: center;justify-content: center;" class="btn centerboth"
						v-if="itm.selected==true" @click="selThis(gIndex)">
						<text class="iconfont car-sel"></text>
					</view>
					<view style="display: flex;align-items: center;justify-content: center;" class="btn centerboth"
						v-else @click="selThis(gIndex)">
						<text class="iconfont car-unsel"></text>
					</view>
					<image class="car-img" :src="itm.thumb" mode="aspectFill"></image>
					<view class="car-mes centerboth">
						<view class="mes-box">
							<view class="g-name line2">{{itm.title}}</view>
							<view class="gz-box"><text>{{itm.attr}}</text></view>
							<view class="price-change-num">
								<view class="price">
									<price :price="itm.sales_price" />
								</view>
								<view class="num-box centerboth">
									<text class="iconfont car-sub"
										@click="changeNum(gIndex,itm.stock,itm.quantity,0,itm.id)"></text>
									<view>{{itm.quantity}}</view>
									<text class="iconfont car-add"
										@click="changeNum(gIndex,itm.stock,itm.quantity,1,itm.id)"></text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view v-if="delBtn == 0" class="car-bottom-btn">
				<view class="all-sel-btn centerboth" @click="allSelBtn">
					<text v-if="allSel==true" class="iconfont car-sel"></text>
					<text v-else class="iconfont car-unsel"></text>
					全选
				</view>
				<view class="all-cost centerboth">
					合计:<price :price="allAmount"></price>
				</view>
				<view class="car-btn-box centerboth" style="display: flex;align-items: center;">
					<view class="js-btn centerboth" style="display: flex;align-items: center;justify-content: center;"
						@click="jsCars">
						结算
					</view>
				</view>
			</view>
			
			<view v-if="delBtn == 1" class="car-bottom-btn">
				<view class="all-sel-btn centerboth" @click="allSelBtn">
					<text v-if="allSel==true" class="iconfont car-sel"></text>
					<text v-else class="iconfont car-unsel"></text>
					全选
				</view>
				<view  class="car-btn-box centerboth" style="display: flex;align-items: center;">
					<view class="del-btn centerboth" style="display: flex;align-items: center;justify-content: center;"
					@click="delCars">删除</view>
				</view>
			</view>
			<u-modal :show="modalShow" @confirm="confirm" :content='maskTitle'></u-modal>
			<!-- <mask-model ref="askmodel" btnType="1" @confirm="confirm" @cancel="cancel" titleColoe="#666666" cancelColor="#666666" confirmColor="#007AFF" :maskTitle="maskTitle"></mask-model> -->
		</view>
	</view>
</template>
<script>
	import price from '../price.vue'
	export default {
		components: {
			price
		},
		props: {
			carList: { //提示信息字体颜色
				type: Array,
				default () {
					return []
				}
			},
			addressName:{
				type: String,
				default () {
					return ''
				}
			},
			amount:{
				type: String,
				default () {
					return ''
				}
			}
		},
		data() {
			return {
				maskTitle: '',
				allSel: true,
				allAmount: '0.00',
				allNumber: 0,
				delIds: '', //要删除的购物车id
				btnType: 0, //0删除  1结算
				modalShow: false,
				delBtn: 0,
				delText:"编辑商品"
			};
		},
		mounted() {
			this.getAllMount(this.amount);
			// this.setAllSel();
		},
		created() {},
		computed: {

		},
		methods: {
			chooseAddress(address){
				console.log(address)
			},
			openPage(path, params) {
				this.$u.route({
					url: path,
					params
				})
			},
			editGoods() {
				if(this.delBtn == 1){
					this.delBtn = 0
					this.delText = "编辑商品"
					// this.setAllSel();
				}else{
					this.delBtn = 1
					this.delText = "完成"
					// this.allSel = false
				}
			},
			confirm: function() { //确定按钮
				var that = this;
				if (that.btnType == 0) {
					that.$emit('delbtn', that.delIds, that.carList);
				} else {
					let ids = that.getCarIds();
					this.$emit('jsbtn', ids)
				}
				
				that.modalShow = false;
			},
			cancel: function() { //取消按钮
				console.log('您点击了取消按钮');
			},
			jsCars: function() {
				let that = this;
				let ids = that.getCarIds();
				this.$emit('jsbtn', ids)
			},
			delCars: function() {
				let that = this;
				let delIds = that.getCarIds();
				
				that.delIds = ''; //清除上次删除记录

				if (!delIds) {
					uni.showToast({
						title: '请选择要删除的商品',
						icon: 'none'
					})
					return false;
				}

				that.delIds = delIds;
				that.btnType = 0;
				that.modalShow = true;
				that.maskTitle = '是否将选中商品移除购物车?'
			},
			selThis: function(gIndex) { //选择商品
				let that = this;
				let carList = that.carList;
				let select = 0;
				if (carList[gIndex].selected == false) {
					carList[gIndex].selected = true;
					select = 1
				} else {
					carList[gIndex].selected = false;
				}

				that.setAllSel();
				that.$emit('selGoods', carList, carList[gIndex].id);
			},
			setAllSel: function() { //是否全选
				let selNum = 0;
				for (let i = 0; i < this.carList.length; i++) {
					if (this.carList[i].selected == true) {
						selNum = selNum + 1;
					}
				}
				if (selNum == this.carList.length && selNum > 0) {
					this.allSel = true;
				} else {
					this.allSel = false;
				}
				this.allNumber = selNum;
				// this.getAllMount();
			},
			allSelBtn: function() { //全选
				var that = this;
				this.allSel = !this.allSel;
				var carList = that.carList;
				for (let k = 0; k < carList.length; k++) {
					if (this.allSel) {
						carList[k].selected = true;
					} else {
						carList[k].selected = false;
					}
				}
				that.$emit('allSelBtn', carList, this.allSel);
			},
			getAllMount: function(allPrice) { //计算选中总价
				var that = this;
				var selNum = 0;
				let carList = that.carList;
				for (let k = 0; k < carList.length; k++) {
					if (carList[k].selected == true) {
						selNum = selNum + carList[k].quantity;
					}
				}
				that.allNumber = selNum;
				that.allAmount = allPrice
			},
			getCarIds: function() { //获取要结算的商品
				var that = this;
				var carList = that.carList;
				var haveSel = [];
				for (let k = 0; k < carList.length; k++) {
					if (carList[k].selected == true) {
						haveSel.push(carList[k].id);
					}
				}
				if (haveSel.length == 0) {
					uni.showToast({
						title: '请选择要操作的商品',
						icon: 'none'
					})
					return false;
				}
				let cartIds = haveSel.join(',');
				return cartIds;
			},
			changeNum: function(gIndex, stock, number, type, id) { //适用于根据数量改变购物车
				var that = this;
				var carList = that.carList;
				if (type == 0) { //减少
					if (number <= 1) {
						return false;
					}
					number = number - 1;
				} else { //增加
					if (number >= stock) {
						uni.showToast({
							title: '库存不足',
							icon: 'none'
						})
						return false;
					}
					number = number + 1;
				}
				that.$emit('changeNum', carList, gIndex, number, id);
			},
		}
	}
</script>

<style scoped>
	@font-face {
		font-family: 'iconfont';
		/* project id 1143282 */
		src: url('https://at.alicdn.com/t/font_1143282_zy7677qexck.eot');
		src: url('https://at.alicdn.com/t/font_1143282_zy7677qexck.eot?#iefix') format('embedded-opentype'),
			url('https://at.alicdn.com/t/font_1143282_zy7677qexck.woff2') format('woff2'),
			url('https://at.alicdn.com/t/font_1143282_zy7677qexck.woff') format('woff'),
			url('https://at.alicdn.com/t/font_1143282_zy7677qexck.ttf') format('truetype'),
			url('https://at.alicdn.com/t/font_1143282_zy7677qexck.svg#iconfont') format('svg');
	}

	.iconfont {
		font-family: "iconfont" !important;
		font-size: 16px;
		font-style: normal;
		-webkit-font-smoothing: antialiased;
		-moz-osx-font-smoothing: grayscale;
	}

	.car-add:before {
		content: "\e8a6";
	}

	.car-sub:before {
		content: "\e8a7";
	}

	.car-no:before {
		content: "\e64d";
	}

	.car-unsel:before {
		content: "\e63a";
	}

	.car-sel:before {
		content: "\e639";
	}

	.car-del:before {
		content: "\e622";
	}

	.car-count {
		width: 100%;
		padding-bottom: 120rpx;
	}

	.manage-btn {
		width: 100%;
		height: 75rpx;
		background: #FFFFFF;
		text-align: right;
	}

	.manage-btn text {
		font-size: 30rpx;
		color: #db422c;
		line-height: 75rpx;
		padding: 0 24rpx;
	}



	.all-sel-btn {
		display: flex;
		align-items: center;
		margin-left: 50rpx;
	}

	.all-sel-btn .iconfont {
		margin-right: 10rpx;
		color: #db422c;
	}

	.car-list {
		/* width: 100%; */
		padding: 24rpx 24rpx 0 24rpx;
	}

	.car-list .list {
		/* width: 100%; */
		padding: 20rpx 20rpx 20rpx 0;
		display: flex;
		background: #FFFFFF;
		border-radius: 10rpx;
		border-bottom: 1px solid #F5F5F5;
	}

	.car-list .list:nth-child(1) {
		border-top-left-radius: 0;
		border-top-right-radius: 0;
	}

	.car-list .list:nth-last-child(1) {
		margin: 0;
	}

	.car-list .list .btn {
		width: 70rpx;
		height: 190rpx;
	}

	.car-list .list .btn .iconfont {
		color: #db422c;
		font-size: 38rpx;
	}

	.car-list .list .car-img {
		width: 190rpx;
		height: 190rpx;
		border-radius: 10rpx;
	}

	.car-list .list .car-mes {
		flex: 1;
		margin-left: 20rpx;
	}

	.car-list .g-name {
		font-size: 28rpx;
		color: #2a2a2a;
		height: 84rpx;
		overflow: hidden;
	}

	.car-list .mes-box {
		width: 100%;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		height: 100%;
	}

	.car-list .gz-box {
		font-size: 26rpx;
		color: #999999;
		margin: 10rpx 0;
	}

	.car-list .mes-box .price {
		color: #ff5621;
		font-weight: bold;
		font-size: 24rpx;
	}

	.car-list .mes-box .yj-price {
		font-size: 36rpx;
	}

	.car-list .price .num {
		color: #2a2a2a;
		font-size: 28rpx;
		font-weight: normal;
		float: right;
	}

	.car-list .num-box {
		display: flex;
		flex-direction: row;
	}

	.car-list .num-box .iconfont {
		font-size: 50rpx;
		color: #999999;
	}

	.car-list .num-box view {
		width: 60rpx;
		text-align: center;
		height: 50rpx;
		line-height: 50rpx;
		font-size: 30rpx;
		color: #2A2A2A;
		margin: 0 10rpx;
	}

	.price-change-num {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.car-bottom-btn {
		position: fixed;
		width: 100%;
		height: 100rpx;
		background: #FFFFFF;
		left: 0;
		bottom: 0;
		z-index: 6;
		box-shadow: 0px -5px 10px -5px #d0d0d0;
		/* padding: 0 300rpx 0 40rpx; */
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.car-bottom-btn .all-cost {
		margin-right: 10rpx;
		display: flex;
		align-items: center;
	}

	.car-bottom-btn .all-cost .icon {
		font-size: 28rpx;
		margin-top: 5rpx;
	}

	.car-bottom-btn text {
		color: #ff5621;
		font-size: 38rpx;
		/* line-height: 80rpx; */
	}

	.car-btn-box {
		/* position: absolute; */
		/* right: 0; */
		height: 60rpx;
		border-radius: 30rpx;
		overflow: hidden;
		z-index: 9;
		margin-right: 20rpx;
		/* top: 0; */
	}

	.car-btn-box view {
		width: 150rpx;
		height: 100%;
		cursor: pointer;
	}

	.del-btn {
		background: red;
		color: #FFFFFF;
	}

	.js-btn {
		/* background: #ff5621; */
		background-image: linear-gradient(135deg, #f2140c, #f2270c 70%, #f24d0c);
		color: #FFFFFF;
	}

	.shop-mes {
		padding: 20rpx;
		background: #FFFFFF;
		border-bottom: 1px solid #F5F5F5;
	}

	.shop-mes .iconfont {
		font-size: 38rpx;
		margin-right: 20rpx;
		color: #ff5621;
	}

	.name-mes {
		display: flex;
		flex-direction: row;
		align-items: center;
	}
</style>
